#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define SQUSIZE 20
typedef int datatype;
typedef struct {            //定义栈
    datatype stack[MAXSIZE];
    datatype top;
}Seqstack,*Seq;
char squence[SQUSIZE][2] = {  //定义运算符
    {'#' , 1} ,
    {')' , 14} ,
    {'*' , 39} ,
    {'/' , 38} ,
    {'+' , 27} ,
    {'-' , 26} ,
    {'(' , 40} ,
};
//预先声明函数
Seq createSeqstack();                       //创建栈函数
void error();                               //异常
void seqAdd(Seq S,int add);                 //入栈
int seqPop(Seq S);                          //出栈
int alculation(Seq S1 , Seq S2 , int a);    //运算函数
int contrast(Seq S , int y);                //比较运算符函数
int Operate(int a,char op,int b);           //计算函数
int pull (char ch );                        //字符转int
char push( int a );                         //int转字符
//预先声明函数
Seq createSeqstack(){     //创建栈函数
    Seq S;
    S =(Seq)malloc(sizeof(Seqstack));
    if(!S){
        //printf("qwe\n");
        error();
    }else{
        S->top=-1;
        return S;
    }
}
void error(){
    printf("Please enter the correct expression\n");
    exit(0);
}
void seqAdd(Seq S,int add){     //入栈
    int id_full = S->top + 1;
    if ( id_full >= SQUSIZE ) {
        // printf("The Seqstack is full\n");
        error();
    }
    S->top++;
    S->stack[S->top] = add;
}
int seqPop(Seq S){  //出栈
    if( S->top == -1){
        // printf("The Seqstack is empty\n");
        error();
        return 0 ;
    }else{
        int result = S->stack[S->top];
        S->top--;
        return result;
    }
}
int alculation(Seq S1 , Seq S2 ){    //运算函数
    int b = seqPop(S1);
    int a = seqPop(S1);
    // contrast( S , y );
    return Operate( a , push(seqPop(S2)) , b );
}
int Operate(int a,char op,int b){
    switch(op){
        case'+':    return a + b;   break;
        case'-':    return a - b;   break;
        case'*':    return a * b;   break;
        case'/':    return a / b;   break;
        default:
            error();
    }
}
int contrast(Seq S1 , Seq S2 , int y){    //比较运算符函数
    int x = S2->stack[S2->top];
    x = x / 10;
    int y1 = y / 10;
    if( x >= y1 && x != 4 && x != 0){
        seqAdd( S1 , alculation( S1 , S2 ) );
        contrast( S1 , S2 , y );
        return 0 ;
    }else if ( x == 4 && y1 == 1) {
        seqPop( S2 );
        return 0 ;
    }else if ( y == pull('#') ){
        return 0;
    }else{
        seqAdd( S2 , y );
        return 0;
    }
}
int pull (char ch ){
    for(int i = SQUSIZE ; i >= 0; i--){
        if( squence[i][0] == ch ){  //记录运算符
            return squence[i][1];
            break;  //如果找到，直接跳出循环
        }
    }
}
char push( int a ){
    for(int i = SQUSIZE ; i >= 0; i--){
        if( squence[i][1] == a ){  //记录运算符
            return squence[i][0];
            break;  //如果找到，直接跳出循环
        }
    }
}
int main(){
    Seq S1 = createSeqstack(); //qwe
    Seq S2 = createSeqstack(); //qwe
    printf("Please enter an expression to be calculated(Start with # and end with #):\n");
    char ch = getchar();
    if( ch == '#' ){
        seqAdd(S2 , pull(ch) );
        while( ( ch = getchar() ) != '#' ){
            int item  = ch - '0';
            if( item >= 0 && item <= 9 ){    //记录数字
                seqAdd(S1 , item);
            }else{
                contrast( S1  ,S2 , pull(ch) );
            }
        }
        contrast( S1  ,S2 , pull(ch) );
    }else{
        error();
    }
    printf( "%d" , S1->stack[S1->top] ) ;
return 0;
}